<?php
/**
 * 商品管理
 * edition ：v1
 * create time：2021-06-08
 * author: acker
 */
namespace App\Admin\Controllers\Goods;


use App\Admin\Actions\Goods\GoodsBatchOffShelf;
use App\Admin\Actions\Goods\GoodsBatchOnShelf;
use App\Admin\Actions\Goods\NetworkGoods;
use App\Http\Controllers\Controller;
use App\Models\DataDictionaryModel;
use App\Models\yb\GoodsWarehouseModel;
use App\Models\yb\GoodsTypeModel;
use Encore\Admin\Form;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;


class WarehouseGoodsController extends Controller
{
    use ModelForm;

    protected $header = '商品库';

    /**
     * 工作状态列表
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('列表');
            $content->body($this->grid());

        });
    }

    /**
     * 新增
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('添加');
            $content->body($this->form());

        });
    }

    /**
     * 编辑
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('编辑');
            $content->body($this->form()->edit($id));

        });
    }

    public function grid(){

        $grid = new Grid(new GoodsWarehouseModel());

        $grid->model()->orderBy('created_at', 'DESC');

        $grid->column('id','商品编号')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('goods_title','商品标题')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('goods_cover', '封面')->display(function ($filed) {
            return empty($filed) ? config('view.oss_zaiwu') : $filed;
        })->lightbox(['width' => 30, 'height' => 30]);

        $grid->column('goods_banner','轮播图')->display(function($filed){
            return empty($filed) ? array() : $filed;
        })->lightbox(['width' => 30, 'height' => 30]);

        $grid->column('goods_type','类型')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : GoodsTypeModel::selectValue('id',$filed);
        });

        $grid->column('goods_specs','规格')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('is_barrel_deposit','是否有桶押金')->using(DataDictionaryModel::Whether, '未知')->dot([
            0 => 'danger',
            1 => 'info',
        ], 'success');

        $grid->column('barrel_deposit','桶押金')->display(function($filed){
            return empty($filed) ? 0 : $filed;
        });

        $grid->column('distribution_fee','奖励配送费')->display(function($filed){
            return empty($filed) ? 0 : $filed;
        });

        $grid->column('weight','权重')->display(function($filed){
            return empty($filed) ? 0 : $filed;
        })->sortable();

        $grid->column('status','状态')->using(DataDictionaryModel::Status, '未知')->dot([
            0 => 'danger',
            1 => 'info',
        ], 'success');

        $grid->column('created_at','创建时间')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        })->sortable();

        //搜索
        $grid->filter(function($filter){
            $filter->column(1/2, function ($filter) {
                $filter->like('goods_name','商品名称');
                $filter->like('goods_specs','商品规格');
                $filter->between('created_at', '创建时间')->datetime();
            });
            $filter->column(1/2, function ($filter) {
                $filter->equal('status', '状态')->select(DataDictionaryModel::Status);
                $filter->equal('goods_type', '类型')->select(GoodsTypeModel::selectOptions());
            });
            $filter->disableIdFilter();
        });

        $grid->disableExport();

        $grid->fixColumns(1, -1);

        $grid->actions(function($actions){
            $actions->disableView();
            $actions->disableDelete();
            if( $actions->row['status'] == 1){
                $actions->disableEdit();
            }
        });

//        $grid->disableActions();

        $grid->tools(function ($tools) {
            $tools->append(new GoodsBatchOnShelf());
            $tools->append(new GoodsBatchOffShelf());
            $tools->append(new NetworkGoods());
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
        });

        $grid->paginate(10);

        return $grid;
    }

    /**
     * @return mixed
     */
    public function form(){

        $form = new Form(new GoodsWarehouseModel());

        $form->text('goods_title', '商品标题')->rules('required|max:150');

        $form->file('goods_cover','封面图')->uniqueName()->removable()->rules('required');

        $form->multipleImage('goods_banner','轮播图')->uniqueName()->removable()->help('多图上传请按住Ctrl键选择多张');

        $form->select('goods_type', '类型')->options(GoodsTypeModel::selectOptions())->rules('required');

        $form->text('goods_specs', '规格')->rules('required|max:150');

        $form->radio('is_barrel_deposit', '是否有桶押金')->options(DataDictionaryModel::Whether)->default('0');

        $form->currency('barrel_deposit', '桶押金')->symbol('￥')->help('如果有桶押金的话、押金金额必须填写');

        $form->currency('distribution_fee', '奖励配送费')->symbol('￥')->rules('required');

        $form->currency('freight', '运费')->symbol('￥')->rules('required');

        $form->radio('is_client_show', '是否在用户端销售')->options(DataDictionaryModel::Whether)->default('0');

        $form->number('weight', '权重')->help('数字越大排序越靠前');

        $form->radio('status', '状态')->options(DataDictionaryModel::GoodsStatus)->default('0');

        $form->editor('goods_detail','详情');

        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
        });

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
        });

        return $form;
    }


}
